<?php
/**
 * 简历解析 Logic
 */

namespace app\common\logic\package;

use app\common\exception\CustomException;
use think\Exception;

/**
 *  压缩包
 * Class ZipArchiveLogic
 * @package app\common\logic
 */
class ZipArchiveLogic extends BaseArchiveLogic {


    public function __construct()
    {
        $this->class = new \ZipArchive();
    }
    public function open($file){
        $res = $this->class->open($file);
        if($res !== true){
            throw new CustomException('暂无法解压此文件');
        }
        return $this;
    }
    /**
     *
     * @param string $dir
     * @return $this|bool
     */
    public function extractTo($dir){
        set_time_limit(0);
        ini_set('memory_limit','100M');
        $this->mkdir($dir);
        $this->class->extractTo($dir);
        $this->resetFilesName($dir);

        return $this;
    }
    public function resetFilesName($path=''){
        $path || $path = $this->dir;
        $files = scandir($path);
        foreach ($files as $k=>$file) {
            if ($file != '.' && $file != '..') {
                if (is_dir($path . '/' . $file)) {
                    $this->resetFilesName($path . '/' . $file);
                } else {
                    $file_name = $path.'/'.$file;
                    $path_info = pathinfo($file_name);
                    $file_name_new = $path.'/'.uniqid().$k.'.'.($path_info['extension']??'');
                    rename($file_name,$file_name_new);
                }
            }
        }
        return true;
    }
}